﻿using AppMachine.Controls.ValidationRules;
using AppMachine.Domain.LogicCom;
using AppMachine.Domain.LogicCom.AppComs;
using AppMachine.EntityFrameworkCore.Repositories.LogicCom;
using AppMachine.EntityFrameworkCore.Repositories.PerpDev;
using PerpheralDevice;
using PerpheralDevice.Units;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Net.NetworkInformation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using YamlDotNet.Serialization;

namespace AppMachine.Controls.Primitive {
    /// <summary>
    /// 输出开关控件
    /// </summary>
    public class PrimDO : Prim {

        protected ComSwitchRW mfComSwitchRW => this.mfAppComPrim as ComSwitchRW;
        static PrimDO() {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(PrimDO), new FrameworkPropertyMetadata(typeof(PrimDO)));            
        }

        /// <summary>
        /// 1. 优先根据ComID加载组件；2. 如果加载失败，再根据 PerpDevID、PerpUnitID加载组件
        /// </summary>
        /// <returns></returns>
        protected override void Load() {
            base.Load();

            this.PerpDevID = this.mfComSwitchRW!.PerpDevID;
            this.PerpUnitID = this.mfComSwitchRW.PerpUnitID;
            this.IsOpen = this.mfComSwitchRW.IsOpen;
            this.TagName = this.mfComSwitchRW.TagName;
            this.IsDisabled = this.mfComSwitchRW.IsDisabled;
            this.IsReversed = this.mfComSwitchRW.IsReversed;
            this.DelayMiliSecs = this.mfComSwitchRW.DelayMiliSecs;
            this.UpdateTarget();
            this.UpdateSource();
        }
        protected override bool Save() {
            if (!this.UpdateSource()) return false;

            this.mfComSwitchRW.IsDisabledSet(this.IsDisabled).IsReversedSet(this.IsReversed).DelayMiliSecsSet(this.DelayMiliSecs ?? 0);

            return base.Save();
        }

        protected override IEnumerable<PerpDev> LoadPerpTypeItemsSource() {
            return PerpDevRepository.GetInstance().GetAll<PerpDev>().Where(it => it.Unit<SwitchRW>().Any());
        }
        protected override IEnumerable<PerpDevUnit> LoadUnitTypeItemsSource(PerpDev perpDev) {
            return this.mfAppComPrim.PerpDev.Unit<SwitchRW>();
        }

        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) {
            ComSwitchRW tempComSwitchRW = AppComRepository.GetInstance().GetByID<ComSwitchRW>(this.ComID);
            if (tempComSwitchRW != null) {
                if (tempComSwitchRW.View().IsOpen ?? false) {
                    tempComSwitchRW.Shut();
                } else {
                    tempComSwitchRW.Open();                    
                }
                this.IsOpen = tempComSwitchRW.View().IsOpen;
            }
        }

        /// <summary>
        /// 输出开关状态(是否开启)
        /// </summary>
        [Description("输出开关状态(是否开启)")]
        public bool? IsOpen {
            get { return (bool?)GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); }
        }
        public static readonly DependencyProperty IsOpenProperty =
            DependencyProperty.Register("IsOpen", typeof(bool?), typeof(PrimDO), new PropertyMetadata(default(bool?), (d, e) => { }));
        
        /// <summary>
        /// 输出开关控制的器件需要延时等待执行完成的时间(毫秒)
        /// 如气缸、真空等有一个缓冲过程，需要等待稳定后，执行检查输出的执行结果
        /// </summary>
        public int? DelayMiliSecs {
            get { return (int?)GetValue(DelayMiliSecsProperty); }
            set { SetValue(DelayMiliSecsProperty, value); }
        }
        public static readonly DependencyProperty DelayMiliSecsProperty =
            DependencyProperty.Register("DelayMiliSecs", typeof(int?), typeof(PrimDO), new PropertyMetadata(default, (d, e) => { }));

        // <summary>
        /// 是否禁用
        /// </summary>
        public bool IsDisabled {
            get { return (bool)GetValue(IsDisabledProperty); }
            set { SetValue(IsDisabledProperty, value); }
        }
        public static readonly DependencyProperty IsDisabledProperty =
            DependencyProperty.Register("IsDisabled", typeof(bool), typeof(PrimDO), new PropertyMetadata(default(bool), (d, e) => { }));

        /// <summary>
        /// 是否反转应用
        /// </summary>
        public bool IsReversed {
            get { return (bool)GetValue(IsReversedProperty); }
            set { SetValue(IsReversedProperty, value); }
        }
        public static readonly DependencyProperty IsReversedProperty =
            DependencyProperty.Register("IsReversed", typeof(bool), typeof(PrimDO), new PropertyMetadata(default(bool), (d, e) => { }));

    }
}


